<template>
  <view class="ikz-glist" :style="'margin-top:' + css_info.margin_top + 'upx;'">
    <view class="ikz-sticky-box" id="ikz-sticky-box">
      <view
        class="ikz-sticky-title"
        v-for="(item, index) in showType.list"
        :key="index"
      >
        <!-- {{current}}{{item.id}} -->
        <text
          class="ikz-textcenter"
          :style="
            current == item.id
              ? 'font-weight: 800;color: #FDAF66;border-bottom:6rpx solid #FDAF66;'
              : ''
          "
          @click="change"
          :data-index="index"
          :data-id="item.id"
          >{{ item.server_name }}</text
        ></view
      >
    </view>
    <view class="ikz-box">
      <view
        class="ikz-sticky-item"
        v-for="(item, index) in showData.list"
        :key="index"
        @click.stop="godetail(item.id)"
        v-if="!(item.status == 2 && countdownTexts[item.id] === '时间已到')"
      >
        <view class="ikz-sticky-top">
          <view class="ikz-sticky-head">
            <image class="ikz-sticky-img" :src="item.avatar"></image>
          </view>
          <view class="ikz-sticky-head-right">
            <view class="ikz-sticky-head-right-box">
              <view class="ikz-sticky-head-name">
                {{ item.nickname }}
              </view>
              <view class="ikz-sticky-head-time">
                {{ item.time }}
              </view>
            </view>
            <view class="ikz-sticky-head-right-box">
              <view class="ikz-sticky-head-card">
                {{ item.type_text }}
              </view>
            </view>
          </view>
        </view>
        <view class="ikz-sticky-content" v-if="item.information">
          备注：{{ item.information }}
        </view>
        <view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.game_platform">
              <view class="ikz-sticky-spot2"></view>
              游戏名称：{{ item.game_platform }}
            </view>
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.game_time">
              <view class="ikz-sticky-spot1"></view>
              游戏时长：{{ item.game_time }}
            </view>
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.receive_limit">
              <view class="ikz-sticky-spot"></view>
              游戏限制：{{ item.receive_limit }}
            </view>
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.expected_delivery_time">
              <view class="ikz-sticky-spot"></view>
              预计交货时间：{{ item.expected_delivery_time }}
            </view>
          </view>

          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.lease_duration">
              <view class="ikz-sticky-spot2"></view>
              租借时长：{{ item.lease_duration }}
            </view>
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.hope_arrive">
              <view class="ikz-sticky-spot2"></view>
              期望送达：{{ item.hope_arrive }}
            </view>
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.receive_time">
              <view class="ikz-sticky-spot2"></view>
              抢单时间： {{ formatTimestamp(item.receive_time) }}
            </view>
          </view>
          <view class="ikz-sticky-start" v-if="item.start">
            <view class="ikz-sticky-spot"></view>
            起点：{{ item.start }}
          </view>
          <view class="ikz-sticky-end">
            <view class="ikz-sticky-start" v-if="item.destination">
              <view class="ikz-sticky-spot1"></view>
              目的地：{{ item.destination }}
            </view>
          </view>

          <view
            style="
              width: 200%;
              height: 1px;
              background-color: #cfcfcf;
              transform: translateX(-10%);
              margin: 20rpx 0rpx 20rpx 0rpx;
            "
          ></view>
          <view style="display: flex; justify-content: space-between">
            <!-- <view>
              订单金额<text class="ikz-orderdetail-list-head-price2"
                >￥{{ item.price }}</text
              >
            </view> -->
            <view>
              预估收益<text class="ikz-orderdetail-list-head-price"
                >￥{{ item.total_fee }}</text
              >
            </view>
          </view>

          <view style="display: flex; margin-top: 10px" v-if="item.status == 2">
            配送状态：<view class="ikz-orderdetail-list-head-price">
              <span :style="{ color: item.overtimeInfo.color }">
                {{ item.overtimeInfo.text }}
              </span>
            </view>
          </view>
          <!-- 
          <view style="display: flex; margin-top: 10px" v-if="item.status == 2">
            下拉刷新：<view class="ikz-orderdetail-list-head-price">
              <span
                :style="{
                  color: item.over_time.color,
                }"
                >获取最新订单时间</span
              >
            </view>
          </view> -->

          <view class="ikz-sticky-buttom">
            <view
              class="ikz-sticky-btn"
              @click.stop="godetail(item.id, index, item)"
              v-if="item.status == 1"
            >
              抢单
            </view>

            <div v-if="item.super_goods">
              <view
                class="ikz-sticky-btn"
                style="background: #f40"
                @click.stop="getCancellation(item)"
                v-if="
                  item.is_run_pick == 1 &&
                  item.run_goods_status == 0 &&
                  item.status != 4 &&
                  item.status != 7 &&
                  item.status != 1 &&
                  item.status != 3 &&
                  item.business_order_id
                "
              >
                取消订单
              </view>
            </div>

            <!-- <div v-if="item.super_goods">
              <view
                class="ikz-sticky-btn"
                style="background: #f40"
                @click.stop="getCancellation(item)"
                v-if="
                  item.is_run_pick == 1 &&
                  item.run_goods_status == 0 &&
                  item.status != 4 &&
                  item.status != 7 &&
                  item.status != 1 &&
                  item.status != 3 &&
                  item.business_order_id
                "
              >
                取消订单
              </view>
            </div> -->

            <view
              class="ikz-sticky-btn"
              style="background: #4caf50; margin-right: 40rpx"
              @click.stop="payment1(item)"
              v-if="
                item.business_order_id > 0 &&
                item.run_pay == 0 &&
                item.status == 2
              "
            >
              支付垫付金
            </view>

            <div v-if="item.super_goods">
              <view
                class="ikz-sticky-btn"
                style="background: #f40"
                @click.stop="getCancellation1(item)"
                v-if="
                  item.status == 2 &&
                  (item.type == 1 || item.type == 2) &&
                  !item.super_goods
                "
              >
                取消订单
              </view>
            </div>

            <div v-if="item.business_order_id">
              <view
                class="ikz-sticky-btn;margin-left: 40rpx"
                style="background: #ff9800; margin-left: 20px"
                @click.stop="orderService(item.id, index, item)"
                v-if="
                  item.status == 2 &&
                  item.is_run_pick == 1 &&
                  item.run_goods_status == 0 &&
                  item.business_order_id > 0 &&
                  item.run_pay > 0
                "
              >
                确认送达
              </view>
            </div>
            <div v-else>
              <view
                class="ikz-sticky-btn"
                style="background: #ff9800; margin-left: 20px"
                @click.stop="orderService(item.id, index, item)"
                v-if="
                  item.status == 2 &&
                  item.run_goods_status == 0 &&
                  item.business_order_id > 0 &&
                  item.run_pay > 0
                "
              >
                确认送达
              </view>
            </div>
            <div
              v-if="
                item.status == 2 &&
                (item.type == 1 || item.type == 2) &&
                !item.super_goods
              "
            >
              <view
                class="ikz-sticky-btn"
                style="background: #ff9800; margin-left: 20px"
                @click.stop="orderService1(item.id, index, item)"
              >
                确认送达
              </view>
            </div>

            <view
              class="ikz-sticky-btn"
              style="background: #76a8fd; margin-left: 40rpx"
              v-if="
                item.is_run_pick == 0 &&
                item.status != 4 &&
                item.status != 7 &&
                item.status != 1 &&
                item.status != 3 &&
                item.business_order_id &&
                item.business_order_id > 0 &&
                item.run_pay > 0
              "
              @click.stop="getGoods(item)"
            >
              确认取货
            </view>

            <view
              class="ikz-sticky-btn"
              style="background: #2196f3"
              v-if="item.status == 7"
            >
              已送达，等待顾客确认收货。
            </view>
            <view
              class="ikz-sticky-btn"
              style="background: #d6d6d6"
              v-if="item.status == 3"
            >
              已完成
            </view>

            <view
              class="ikz-sticky-btn"
              style="background: #d6d6d6"
              v-if="item.status == 4"
            >
              已取消
            </view>
          </view>
        </view>

        <view
          class="ikz-sticky-btn1"
          v-if="
            item.business_order_id > 0 && item.run_pay == 0 && item.status == 2
          "
        >
          <uni-icons
            type="info"
            size="16"
            color="#f40"
            style="margin-right: 5px"
          ></uni-icons>
          请在{{
            countdownTexts[item.id]
          }}完成垫付，否则系统将会收回订单，如用户中途取消订单将无法获取任何收益。
        </view>
        <view class="ikz-sticky-btn1" v-if="item.run_goods_status == 1">
          <uni-icons
            type="info"
            size="16"
            color="#f40"
            style="margin-right: 5px"
          ></uni-icons>
          {{ "该订单已被骑手取消" }}
        </view>
        <view
          class="ikz-sticky-btn1"
          v-if="
            item.run_goods_status == 2 && item.type == 1 && item.super_goods
          "
        >
          <uni-icons
            type="info"
            size="16"
            color="#f40"
            style="margin-right: 5px"
          ></uni-icons>
          {{ "商家已确认收货" }}
        </view>

        <!-- 没有超时被取消 -->
        <div v-if="item.type == 1 && item.super_goods">
          <view
            class="ikz-sticky-btn1"
            v-if="item.status == 4 && item.run_business_status > 0"
          >
            <view
              class="ikz-sticky-btn1"
              v-if="item.status == 4 && item.run_business_status > 0"
            >
              <uni-icons
                v-if="cancelOrderMessage(item)"
                type="info"
                size="16"
                color="#f40"
                style="margin-right: 5px"
              ></uni-icons>
              {{ cancelOrderMessage(item) }}
            </view>
          </view>
        </div>
      </view>
      <view class="ikz-more" v-if="moreStatus == 4"> ——我也是有底线的—— </view>
      <view class="ikz-more" v-if="moreStatus == 5">
        <image class="ikz-empty" src="/static/empty.png"></image>
      </view>
    </view>
  </view>
</template>

<script>
import ikz from "@/pages/Common/tools";
export default {
  name: "receiverglist",
  props: {
    showType: {
      type: Object,
    },
    showData: {
      type: Object,
    },
    cssData: {
      type: Object,
    },
    moreStatus: {
      type: String,
    },
    typeIndex: {
      type: String,
    },
    receiver: {
      type: String,
    },
  },
  computed: {
    cancelOrderMessage() {
      return (item) => {
        if (item.run_business_status !== 1) {
          return "商家已确认收货，垫付已退回";
        }

        // 检查数据是否存在
        if (!item.cancel_time || !item.hope_arrive) {
          return "用户已取消订单，如果已取货，请将商品退回商家完成订单，否则无法正常退回垫付费用,如未取货，请联系商家完成订单";
        }

        // 解析时间
        const cancelTime = item.cancel_time * 1000; // 取消时间（毫秒）
        const hopeArriveParts = item.hope_arrive.split(" ");
        if (hopeArriveParts.length < 2) {
          return "用户已取消订单，请将商品退回商家完成订单，否则无法正常退回垫付费用,用户已取消订单，如果已取货，请将商品退回商家完成订单，否则无法正常退回垫付费用";
        }

        // 解析预计送达结束时间
        const hopeArriveEnd = new Date(
          hopeArriveParts[0] + "T" + hopeArriveParts[1].split("-")[1] + ":00"
        ).getTime(); // 期望送达结束时间（毫秒）

        // 检查时间是否有效
        if (isNaN(hopeArriveEnd)) {
          return "用户已取消订单，如果已取货，请将商品送回完成订单，否则无法正常退回垫付费用，如未取货请联系商家退款垫付。";
        }

        // 判断是否超时
        if (cancelTime < hopeArriveEnd) {
          // 如果取消时间在预计送达结束时间之前，说明未超时
          return "用户已取消订单，已为你补偿双倍配送费，请将商品退回商家完成订单，否则无法正常退回垫付费用。";
        } else {
          // 如果取消时间在预计送达结束时间之后，计算是否超时10分钟以上
          const diff = cancelTime - hopeArriveEnd; // 取消时间与期望送达结束时间的差值
          if (diff > 10 * 60 * 1000) {
            // 超时10分钟以上
            return item.status == 2
              ? "该订单已超时，用户已取消，请将商品送回商家，否则系统无法为你退回垫付。"
              : "";
          } else {
            return "用户已取消订单，已为你补偿双倍配送费，请将商品退回商家完成订单，否则无法正常退回垫付费用。";
          }
        }
      };
    },
  },
  data() {
    return {
      duration: 500,
      current: 0,
      css_info: {
        margin_top: 0,
      },
      winHeight: 0,
      scroll_status: true,
      scrollTop: 0,
      timers: {}, // 修改：为每个订单项存储独立的定时器
      countdownTexts: {}, // 修改：为每个订单项存储独立的倒计时文本

      overtimeInfo: {}, // 用于存储每个订单的配送状态信息
      intervalTimer: null, // 定时器
    };
  },
  watch: {
    "showData.list": {
      handler(newList) {
        if (newList && newList.length > 0) {
          newList.forEach((item) => {
            if (item.status == 2 && item.receive_time && item.hope_arrive) {
              // 初始化配送状态
              this.$set(
                item,
                "overtimeInfo",

                this.calculateOverTime1(
                  item.receive_time,
                  this.formatDateTime(item.hope_arrive)
                )
              );
            }
          });
        }
      },
      immediate: true,
      deep: false, // 移除深度监听
    },
    showData: {
      handler(newData) {
        if (newData && newData.list) {
          newData.list.forEach((item) => {
            if (
              item.business_order_id > 0 &&
              item.run_pay == 0 &&
              item.receive_time
            ) {
              console.log(11);
              this.startCountdown(item.id, item.receive_time); // 为每个订单启动倒计时
            }
          });
        }
      },
      immediate: true,
      deep: true,
    },
    typeIndex: {
      handler(newData) {
        console.log(newData, "newData");
        this.current = newData;
      },
    },
  },
  beforeDestroy() {
    // this.clearInterval(); // 清除定时器
    this.clearInterval1(); // 清除定时器
  },
  mounted() {
    this.startInterval(); // 启动定时器
    const query = uni.createSelectorQuery().in(this);
    query
      .select("#ikz-sticky-box")
      .boundingClientRect((data) => {
        this.$emit("returnglist", data.top);
      })
      .exec();
    var that = this;
    uni.getSystemInfo({
      success: function (res) {
        that.winHeight = res.windowHeight;
      },
    });
  },
  methods: {
    clearInterval1() {
      if (this.intervalTimer) {
        clearInterval1(this.intervalTimer); // 清除定时器
        this.intervalTimer = null;
      }
    },
    // ... existing methods ...
    startInterval() {
      this.intervalTimer = setInterval(() => {
        if (this.showData.list && this.showData.list.length > 0) {
          this.showData.list.forEach((item) => {
            if (item.status == 2 && item.receive_time && item.hope_arrive) {
              // 更新配送状态
              this.$set(
                item,
                "overtimeInfo",
                this.calculateOverTime1(
                  item.receive_time,
                  this.formatDateTime(item.hope_arrive)
                )
              );
            }
          });
        }
      }, 1000); // 每秒更新一次
    },
    calculateOverTime1(receiveTime, hopeArrive) {
        //console.log(receiveTime, hopeArrive, "ddddddddddd");
      // 如果缺少必要参数，返回默认值
      if (!receiveTime || !hopeArrive)
        return { text: "正常配送中", color: "green" };

      // 解析预计送达时间范围
      const [date, timeRange] = hopeArrive.split(" ");
      const [startTime, endTime] = timeRange.split("-");

      // 构造预计送达时间结束点
      const hopeArriveEndDate = new Date(`${date}T${endTime}:00`);
      const now = new Date();

      // 如果当前时间小于预计送达时间结束点，返回正常配送中
      if (now < hopeArriveEndDate)
        return { text: "正常配送中...", color: "green" };

      // 计算超时时间
      const diff = now - hopeArriveEndDate;
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      const hours = Math.floor(
        (diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
      );
      const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((diff % (1000 * 60)) / 1000);

      let result = "已超时：";

      // console.log(days, hours, minutes, seconds, "222222222");

      if (days > 0) result += `${days}天`;
      if (hours > 0) result += `${hours}小时`;
      if (minutes > 0) result += `${minutes}分钟`;
      if (seconds > 0) result += `${seconds}秒`;
      return { text: result, color: "red" };
    },
    formatDateTime(input) {
      // 使用正则表达式匹配日期和时间部分
      const match = input.match(
        /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})$/
      );
      if (!match) {
        return input; // 如果不是目标格式，直接返回原字符串
      }

      // 提取年、月、日和时间部分
      const [_, year, month, day, startHour, startMinute, endHour, endMinute] =
        match;

      // 格式化月份和日期为两位数
      const formattedMonth = String(month).padStart(2, "0");
      const formattedDay = String(day).padStart(2, "0");

      // 格式化时间部分
      const formattedStartHour = String(startHour).padStart(2, "0");
      const formattedEndHour = String(endHour).padStart(2, "0");

      // 返回格式化后的字符串
      return `${year}-${formattedMonth}-${formattedDay} ${formattedStartHour}:${startMinute}-${formattedEndHour}:${endMinute}`;
    },
    // 启动倒计时
    startCountdown(orderId, receiveTime) {
      console.log(2);
      if (this.timers[orderId]) {
        clearInterval(this.timers[orderId]); // 清除之前的定时器
      }

      const endTime = receiveTime * 1000 + 3 * 60 * 1000; // 抢单时间 + 3分钟
      this.updateCountdown(orderId, endTime); // 立即更新一次

      this.timers[orderId] = setInterval(() => {
        this.updateCountdown(orderId, endTime);
      }, 1000); // 每秒更新一次
    },

    // 更新倒计时
    updateCountdown(orderId, endTime) {
      const now = Date.now();
      const diff = endTime - now;

      if (diff <= 0) {
        if (this.timers[orderId]) {
          clearInterval(this.timers[orderId]); // 清除定时器
          delete this.timers[orderId]; // 删除定时器引用
        }
        if (
          !this.countdownTexts[orderId] ||
          this.countdownTexts[orderId] !== "时间已到"
        ) {
          this.$set(this.countdownTexts, orderId, "时间已到");

          // 手动删除对应的订单数据
          const index = this.showData.list.findIndex(
            (item) => item.id === orderId
          );
          if (index !== -1) {
            this.showData.list.splice(index, 1); // 删除数据
            this.$forceUpdate(); // 强制更新视图
          }

          this.$emit("loadMore"); // 触发 loadMore 事件
          console.log(3); // 仅打印一次
          this.$forceUpdate(); // 强制更新视图
        }
        return;
      }

      const minutes = Math.floor(diff / (1000 * 60));
      const seconds = Math.floor((diff % (1000 * 60)) / 1000);

      this.$set(this.countdownTexts, orderId, `${minutes}分${seconds}秒`);
    },
    godetail(id) {
      uni.navigateTo({
        url:
          "/pages/Component/Module/Receiver/Program/order/detail/index?id=" +
          id,
      });
    },
    change: function (e) {
      this.current = e.currentTarget.dataset.index;
      this.$emit("returnId", e.currentTarget.dataset.id);
    },
    orderHelp(id, index, item) {
      const _this = this;
      var path = "/xyb/order_receive/add";
      var data = {
        orderid: id,
      };
      ikz.post(path, data, (res) => {
        if (res.data.code) {
          uni.showToast({
            title: "抢单成功",
            duration: 2000,
          });
          _this.$emit("loadMore");
          _this.$forceUpdate();
          if (item.business_order_id) {
            uni.showModal({
              title: "提示",
              content: "该订单需要垫付，请支付后抢单",
              success(res) {
                if (res.confirm) {
                  _this.payment(item);
                }
              },
            });
          }

          return;
        } else {
          uni.showModal({
            title: "提示",
            content: res.data.msg,
            showCancel: false,
            success: function (res) {},
          });
        }
      });
    },
    orderService1(id, index, item) {
      const _this = this;
      // // console.log(item,'itemitemitem');
      // // 判断是否需要垫付,以及骑手是否支付了
      // const _this = this;
      // if (item.business_order_id > 0 && item.run_pay == 0) {
      //   uni.showModal({
      //     title: "提示",
      //     content: "该订单需要垫付，请支付后抢单",
      //     success(res) {
      //       if (res.confirm) {
      //         _this.payment(item);
      //       }
      //     },
      //   });
      //   return;
      // }
      uni.showModal({
        title: "提示",
        content: "该订单是否确认已送达？",
        success(res) {
          if (res.confirm) {
            var path = "/xyb/order_receive/receive_confirm_arrive";
            var data = {
              orderid: id,
            };
            ikz.post(path, data, (res) => {
              if (res.data.code) {
                uni.showToast({
                  title: "送达成功",
                  duration: 2000,
                });
                _this.$emit("loadMore");
                _this.$forceUpdate();
              } else {
                uni.showModal({
                  title: "提示",
                  content: res.data.msg,
                  showCancel: false,
                  success: function (res) {},
                });
              }
            });
          }
        },
      });

      // console.log(1);
      // return;
    },
    orderService(id, index, item) {
      // console.log(item,'itemitemitem');
      // 判断是否需要垫付,以及骑手是否支付了
      const _this = this;
      if (item.business_order_id > 0 && item.run_pay == 0) {
        uni.showModal({
          title: "提示",
          content: "该订单需要垫付，请支付后抢单",
          success(res) {
            if (res.confirm) {
              _this.payment(item);
            }
          },
        });
        return;
      }
      uni.showModal({
        title: "提示",
        content: "该订单是否确认已送达？",
        success(res) {
          if (res.confirm) {
            var path = "/xyb/order_receive/receive_confirm_arrive";
            var data = {
              orderid: id,
            };
            ikz.post(path, data, (res) => {
              if (res.data.code) {
                uni.showToast({
                  title: "送达成功",
                  duration: 2000,
                });
                _this.$emit("loadMore");
                _this.$forceUpdate();
              } else {
                uni.showModal({
                  title: "提示",
                  content: res.data.msg,
                  showCancel: false,
                  success: function (res) {},
                });
              }
            });
          }
        },
      });

      // console.log(1);
      // return;
    },
    getCancellation1(item) {
      const _this = this;
      uni.showModal({
        title: "确认取消",
        content: "确认取消订单吗？",
        success(res) {
          if (res.confirm) {
            var path = "/xyb/order/runCancel";
            var data = {
              id: item.id,
            };
            ikz.post(path, data, (res) => {
              if (res.data.code) {
                uni.showToast({
                  title: "取消成功",
                  duration: 2000,
                });
                _this.$emit("loadMore");
                _this.$forceUpdate();
              } else {
                uni.showModal({
                  title: "提示",
                  content: res.data.msg,
                  showCancel: false,
                });
              }
            });
          }
        },
      });
    },
    // 骑手取消订单
    getCancellation(item) {
      const _this = this;
      uni.showModal({
        title: "确认取消",
        content:
          item.run_pay == 1
            ? "确认取消订单吗？取消订单将会扣除20%的垫付金额"
            : "确认取消订单吗？",
        success(res) {
          if (res.confirm) {
            var path = "/xyb/order/runCancel";
            var data = {
              id: item.id,
            };
            ikz.post(path, data, (res) => {
              if (res.data.code) {
                uni.showToast({
                  title: "取消成功",
                  duration: 2000,
                });
                _this.$emit("loadMore");
                _this.$forceUpdate();
              } else {
                uni.showModal({
                  title: "提示",
                  content: res.data.msg,
                  showCancel: false,
                });
              }
            });
          }
        },
      });
    },
    formatTimestamp(timestamp) {
      if (!timestamp) return "";
      const date = new Date(timestamp * 1000);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },

    // 调起小程序微信支付
    payment(item) {
      const _this = this;
      if (this.item.run_pay == 0) {
        // this.$emit('typeInfo');
        uni.showModal({
          title: "提示",
          content: "该订单需要骑手垫付，是否前往支付？",
          success: function (res) {
            if (res.confirm) {
              uni.showLoading({
                mask: true,
                title: "正在请求支付...",
              });
              var path = "/xyb/order/run_wx_minapp_pay";
              var data = {
                orderid: item.id,
                amount: item.super_goods.total_fee,
              };
              ikz.post(path, data, (res) => {
                console.log("调起小程序微信支付 ", res);
                uni.hideLoading();
                if (res.data.code == 1) {
                  wx.requestPayment({
                    timeStamp: res.data.data.timeStamp,
                    nonceStr: res.data.data.nonceStr,
                    package: res.data.data.package,
                    signType: res.data.data.signType,
                    paySign: res.data.data.paySign,
                    success: (res) => {
                      console.log("success", res);
                      if (res.errMsg == "requestPayment:ok") {
                        uni.showToast({
                          title: "支付成功",
                          duration: 2000,
                          icon: "success",
                        });
                        _this.$emit("loadMore");
                        _this.$forceUpdate();
                      }
                    },
                    fail: (res) => {
                      uni.showModal({
                        title: "提示",
                        showCancel: false,
                        content: "订单已创建，请及时进行支付",
                      });
                    },
                    complete: (res) => {
                      console.log("complete", res);
                    },
                  });
                } else {
                  uni.showToast({
                    title: res.data.msg,
                    icon: "none",
                  });
                }
              });
            }
          },
        });
        return;
      }
    },

    // 调起小程序微信支付
    payment1(item) {
      const _this = this;
      if (item.run_pay == 0) {
        uni.showLoading({
          mask: true,
          title: "正在请求支付...",
        });
        var path = "/xyb/order/run_wx_minapp_pay";
        var data = {
          orderid: item.id,
          amount: item.super_goods.total_fee,
        };
        ikz.post(path, data, (res) => {
          console.log("调起小程序微信支付 ", res);
          uni.hideLoading();
          if (res.data.code == 1) {
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: res.data.data.signType,
              paySign: res.data.data.paySign,
              success: (res) => {
                console.log("success", res);
                if (res.errMsg == "requestPayment:ok") {
                  uni.showToast({
                    title: "支付成功",
                    duration: 2000,
                    icon: "success",
                  });
                  _this.$emit("loadMore");
                  _this.$forceUpdate();
                }
              },
              fail: (res) => {
                uni.showModal({
                  title: "提示",
                  showCancel: false,
                  content: "订单已创建，请及时进行支付",
                });
              },
              complete: (res) => {
                console.log("complete", res);
              },
            });
          } else {
            uni.showToast({
              title: res.data.msg,
              icon: "none",
            });
          }
        });
        return;
      }
    },

    //骑手取货
    getGoods(item) {
      const _this = this;
      // 判断给钱了吗
      if (item.business_order_id > 0 && item.run_pay == 0) {
        uni.showModal({
          title: "提示",
          content: "该订单需要垫付，请支付后取货",
          success(res) {
            if (res.confirm) {
              _this.payment(item);
            }
          },
        });
        return;
      }
      this.quhuo(item);
    },
    quhuo(item) {
      const _this = this;
      uni.showModal({
        title: "取件码",
        content: "",
        editable: true,
        success(res) {
          if (res.confirm) {
            if (!res.content || res.content.trim() === "") {
              uni.showToast({
                title: "取件码不能为空",
                icon: "none",
                duration: 2000,
              });
              return;
            }
            // console.log(item, "item", res.content);
            var path = "/xyb/order/pickCode";
            var data = {
              id: item.id,
              run_pick_code: res.content,
            };
            ikz.post(path, data, (res) => {
              if (res.data.code) {
                uni.showToast({
                  title: "取件成功",
                  duration: 2000,
                });
                _this.$emit("loadMore");
                _this.$forceUpdate();
              } else {
                uni.showModal({
                  title: "提示",
                  content: res.data.msg,
                  showCancel: false,
                });
              }
            });
          }
        },
      });
    },
    // 调起小程序微信支付
    payment(item) {
      const _this = this;
      if (item.business_order_id > 0 && item.run_pay == 0) {
        // this.$emit('typeInfo');
        uni.showModal({
          title: "提示",
          content: "该订单需要骑手垫付，是否前往支付？",
          success: function (res) {
            if (res.confirm) {
              uni.showLoading({
                mask: true,
                title: "正在请求支付...",
              });
              var path = "/xyb/order/run_wx_minapp_pay";
              var data = {
                orderid: item.id,
                amount: item.super_goods.total_fee,
              };
              ikz.post(path, data, (res) => {
                console.log("调起小程序微信支付 ", res);
                uni.hideLoading();
                if (res.data.code == 1) {
                  wx.requestPayment({
                    timeStamp: res.data.data.timeStamp,
                    nonceStr: res.data.data.nonceStr,
                    package: res.data.data.package,
                    signType: res.data.data.signType,
                    paySign: res.data.data.paySign,
                    success: (res) => {
                      console.log("success", res);
                      if (res.errMsg == "requestPayment:ok") {
                        uni.showToast({
                          title: "支付成功",
                          duration: 2000,
                          icon: "success",
                        });
                        _this.$emit("loadMore");
                      }
                    },
                    fail: (res) => {
                      uni.showModal({
                        title: "提示",
                        showCancel: false,
                        content: "订单已创建，请及时进行支付",
                      });
                    },
                    complete: (res) => {
                      console.log("complete", res);
                    },
                  });
                } else {
                  uni.showToast({
                    title: res.data.msg,
                    icon: "none",
                  });
                }
              });
            }
          },
        });
        return;
      }
    },
  },
};
</script>

<style>
.ikz-sticky-box {
  /* #ifndef APP-PLUS-NVUE */
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: -webkit-sticky;
  /* #endif */
  position: sticky;
  top: var(--window-top);
  z-index: 999;
  flex-direction: row;
  margin: 0px;
  padding: 15px 0 15px 0;
  white-space: nowrap;
  background: #fff;
}

.ikz-sticky-title {
  box-sizing: border-box;
  padding: 0 20rpx;
  display: inline-block;
  text-align: center;
}

.ikz-textcenter {
  padding-bottom: 10rpx;
}

/* list */
.ikz-sticky-item-box {
  overflow-x: auto;
}

.ikz-sticky-item {
  box-sizing: border-box;
  padding: 30rpx;
  width: 95%;
  margin: 0 auto 20rpx;
  background: #ffffff;
  border-radius: 15rpx;
}

.ikz-sticky-top {
  display: flex;
  align-items: center;
}

.ikz-sticky-img {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
}

.ikz-sticky-head-right {
  margin-left: 20rpx;
  width: 85%;
}

.ikz-sticky-head-right-box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 50rpx;
}

.ikz-orderdetail-list-head-price {
  color: #f44336;
}

.ikz-sticky-head-time {
  color: #888;
  font-size: 25rpx;
}

.ikz-sticky-head-card {
  background-color: #f44336;
  color: #fff;
  font-size: 23rpx;
  border-radius: 15rpx;
  padding: 0px 10rpx;
  text-align: center;
  line-height: 45rpx;
}

.ikz-orderdetail-list-head-price {
  color: #f44336;
}
.ikz-orderdetail-list-head-price2 {
  color: #ff5722;
}

.ikz-sticky-content {
  line-height: 45rpx;
  font-size: 30rpx;
  margin: 15rpx 0;
}

.ikz-sticky-head-name {
  font-size: 28rpx;
}

.ikz-sticky-start {
  display: flex;
  align-items: center;
  font-size: 28rpx;
  line-height: 50rpx;
}

.ikz-sticky-spot {
  width: 20rpx;
  height: 20rpx;
  border-radius: 50%;
  background-color: #76a8fd;
  margin-right: 20rpx;
}

.ikz-sticky-spot1 {
  width: 20rpx;
  height: 20rpx;
  border-radius: 50%;
  background-color: #ff8800;
  margin-right: 20rpx;
}
.ikz-sticky-spot2 {
  width: 20rpx;
  height: 20rpx;
  border-radius: 50%;
  background-color: #009423;
  margin-right: 20rpx;
}

.ikz-sticky-end {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ikz-sticky-buttom {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 20px;
}
.ikz-sticky-btn {
  background: #42d5be;
  color: #fff;
  padding: 0 40rpx;
  border-radius: 15rpx;
  line-height: 50rpx;
  font-size: 26rpx;
}
.ikz-sticky-btn1 {
  color: #ff5722;
  margin-top: 20rpx;
  font-size: 26rpx;
}
.ikz-more {
  width: 100%;
  text-align: center;
  line-height: 50rpx;
  color: #999999;
  padding-bottom: 25rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 25rpx;
}
.ikz-empty {
  width: 150rpx;
  height: 150rpx;
  margin-bottom: 20rpx;
  margin-top: 150rpx;
}
</style>
